using AvalonAdmin.BaseViewModel;
using AvalonAdmin.Consts;
using AvalonAdmin.Helpers;
using AvalonAdminDto.CarParts.CarPartsTypeDtos;
using AvalonAdminDto.Common;
using AvalonAdminDto.Sys.AdminDtos;
using D.UtilCore.Dtos;
using System;
using System.Threading.Tasks;

namespace AvalonAdmin.Pages.CarParts.ViewModels.VehicleSeriesCarPartsType
{
    /// <summary>
    /// 车系配件类型修改视图模型
    /// </summary>
    public class VehicleSeriesCarPartsTypeModifyViewModel : BaseModifyViewModel
    {
        #region 页面属性及初始化数据

        private string? _id; 
        public VehicleSeriesCarPartsTypeModifyViewModel(string? id, ViewStatus? viewStatus)
            : base()
        {
            _id = id; 
            _currentVehicleSeriesCarPartsType = new RepVehicleSeriesCarPartsType();
            SetupFormStatus(viewStatus);

            // 如果有ID，则加载数据
            if (!string.IsNullOrEmpty(_id))
            {
                _ = Task.Run(async () => await QueryAsync());
            }
        }

        /// <summary>
        /// 查询数据（用于刷新当前用户数据）
        /// </summary>
        public override async Task<bool> QueryAsync()
        {
            if (!string.IsNullOrEmpty(_id))
            {
                try
                {
                    var res = await HttpHelper<RepVehicleSeriesCarPartsType>.GetAsync($"{ApiUrl.GetVehicleSeriesCarPartsType}/{_id}");

                    if (res.Code == StatusCodeEnum.OK && res.Data != null)
                    {
                        _id = res.Data.Id;
                        //CurrentUser = new RepAdminInfo
                        //{
                        //    Id = res.Data.Id,
                        //    UUID = res.Data.UUID,
                        //    Account = res.Data.Account,
                        //    NickName = res.Data.NickName,
                        //    Email = res.Data.Email,
                        //    Phone = res.Data.Phone,
                        //    Status = res.Data.Status,
                        //    Description = res.Data.Description,
                        //    CreateUser = res.Data.CreateUser,
                        //    CreateTime = res.Data.CreateTime,
                        //    UpdateUser = res.Data.UpdateUser,
                        //    UpdateTime = res.Data.UpdateTime
                        //}; 
                    }
                    else if (res.Code == StatusCodeEnum.Waring)
                    {
                        ShowWarning(res.Msg);
                    }
                    else
                    {
                        ShowError(res.Msg ?? "加载车系配件类型数据失败");
                    }
                }
                catch (System.Exception ex)
                {
                    ShowError($"加载车系配件类型数据失败：{ex.Message}");
                }
            }
            return true;
        }
        #endregion

        #region 数据编辑属性

        private RepVehicleSeriesCarPartsType _currentVehicleSeriesCarPartsType;

        private string vehicleSeriesId;
        /// <summary>
        /// 车系ID
        /// </summary>
        public string? VehicleSeriesId
        {
            get => vehicleSeriesId;
            set => SetProperty(ref vehicleSeriesId, value);
        }

        private string carPartsTypeId;
        /// <summary>
        /// 配件类型ID
        /// </summary>
        public string? CarPartsTypeId
        {
            get => carPartsTypeId;
            set => SetProperty(ref carPartsTypeId, value);
        }

        private string remark;
        /// <summary>
        /// 备注
        /// </summary>
        public string? Remark
        {
            get => remark;
            set => SetProperty(ref remark, value);
        }
        #endregion

        #region 提交，重置

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <returns></returns>
        protected override async Task SubmitAsync()
        {
            try
            {
                // 参数验证判断
                if (string.IsNullOrWhiteSpace(VehicleSeriesId))
                {
                    ShowWarning("车系ID不能为空"); return;
                }

                // 参数验证判断
                if (string.IsNullOrWhiteSpace(CarPartsTypeId))
                {
                    ShowWarning("配件类型ID不能为空"); return;
                }

                if (string.IsNullOrEmpty(_id))
                {
                    // 调用后端接口
                    var res = await HttpHelper<int>.PostAsync(ApiUrl.AddVehicleSeriesCarPartsType, new ReqAddVehicleSeriesCarPartsType
                    {
                        VehicleSeriesId = VehicleSeriesId,
                        CarPartsTypeId = CarPartsTypeId,
                    });
                }
                else
                {
                    // 调用后端接口 
                    var res = await HttpHelper<int>.PostAsync(ApiUrl.UpdateVehicleSeriesCarPartsType, new ReqUpdateVehicleSeriesCarPartsType
                    {
                        Id = _id,
                        VehicleSeriesId = VehicleSeriesId,
                        CarPartsTypeId = CarPartsTypeId,
                    });
                    HandleApiResponse(res, "添加成功", "添加失败");
                }
            }
            catch (Exception ex)
            {
                ShowError($"操作失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 重置表单
        /// </summary>
        protected override void Reset()
        {
            // 实现重置表单逻辑
            _currentVehicleSeriesCarPartsType = new RepVehicleSeriesCarPartsType();
            OnPropertyChanged(nameof(VehicleSeriesId));
            OnPropertyChanged(nameof(CarPartsTypeId));
            OnPropertyChanged(nameof(Remark));
        }
        #endregion
    }
}